package com.aistar.hotel.service.impl;

import com.aistar.common.util.IdWorker;
import com.aistar.hotel.entity.Hotel;
import com.aistar.hotel.mapper.HotelMapper;
import com.aistar.hotel.service.HotelMapperService;
import com.github.pagehelper.PageHelper;
import com.github.pagehelper.PageInfo;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.data.redis.core.RedisTemplate;
import org.springframework.stereotype.Service;
import org.springframework.transaction.annotation.Transactional;

import java.util.List;


/**
 * Created by wubin on 2019/6/23.
 */
@Service
@Transactional
public class HotelMapperServiceImpl implements HotelMapperService {

@Autowired
private HotelMapper hotelMapper;
@Autowired
private RedisTemplate redisTemplate;
@Autowired
private IdWorker idWorker;

    @Override
    public List<Hotel> findAll(String hotelname) {
        List<Hotel> hotels = (List<Hotel>) redisTemplate.opsForValue().get("hotel_all");
        if (null==hotels){
            hotels = hotelMapper.findAll("");
            redisTemplate.opsForValue().set("hotel_all",hotels);
        }


        return hotels;
    }

    @Override
    public Hotel findById(String id) {
        Hotel hotel = (Hotel) redisTemplate.opsForValue().get("hotel_"+id);
        if (null==hotel){
            hotel = hotelMapper.findById(id);
            redisTemplate.opsForValue().set("hotel_"+id,hotel);

        }

        return hotel;
    }

    @Override
    public List<Hotel> findByany(Hotel hotel) {
        return hotelMapper.findByany(hotel);
    }

    @Override
    public void save(Hotel hotel) {


        hotelMapper.save(hotel);
    }

    @Override
    public void delete(String id) {
  hotelMapper.delete(id);
    }

    @Override
    public void update(Hotel hotel) {

        hotelMapper.update(hotel);
    }

    @Override
    public PageInfo<Hotel> getPageInfo(String hotelname, Integer pageNow, Integer pageSize) {
//        pageNow是指当前的页数，pageSize是指每页显示的条数，在这里进行了判断，分别判断了当这两个参数都为null的时候，给与的默认值
        PageHelper.startPage(pageNow==null?1:pageNow,pageSize==null?3:pageSize);//第二页,每页显示三条


        //需要分页的数据 - 带条件的
        //如果没有输入用户名 - 那么就查询所有的，这里也进行了参数的判断，如果username不存在那么就给与空字符串
//        也就是默认查询所有的，如果有那么就查询所要的，并且进行去空格的操做
        List<Hotel> hotels = hotelMapper.findAll(hotelname==null?"":hotelname.trim());

        PageInfo<Hotel> pageInfo = new PageInfo<>(hotels);//将查询的对象放入发哦分页的插件对象中


        return pageInfo;//返回分页的插件，在测试时，是要拿出分页对象中的数据(user的集合)pageInfo.getlist
    }
}
